package cn.blog.controller;

import java.util.List;

import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import cn.blog.common.LayuiTableResult;
import cn.blog.common.PublicResultJosn;
import cn.blog.entity.Match;
import cn.blog.service.MatchService;

/**
 * 比赛管理
 * 
 * @author An-Il
 *
 */
@RestController
@RequestMapping("/match")
public class MatchController {

	@Autowired
	private MatchService matchService;

	/**
	 * 添加一场比赛信息
	 * 
	 * @param match
	 * @return
	 */
	@PostMapping
	public PublicResultJosn add(@RequestBody Match match) {
		PublicResultJosn resultJosn = matchService.add(match);
		return resultJosn;
	}

	/**
	 * 单、批量删除比赛信息
	 * 
	 * @param ids
	 * @return
	 */
	@DeleteMapping
	public PublicResultJosn delete(@RequestBody List<String> ids) {
		// 校验
		if (CollectionUtils.isEmpty(ids)) {
			throw new IllegalArgumentException("删除id参数不能为空");
		}
		PublicResultJosn resultJosn = matchService.delete(ids);
		return resultJosn;
	}

	/**
	 * 更新一场比赛信息
	 * 
	 * @param match
	 * @return
	 */
	@PutMapping
	public PublicResultJosn update(@RequestBody Match match) {
		// 校验
		if (StringUtils.isBlank(match.getMatchid())) {
			throw new IllegalArgumentException("id参数异常");
		}
		PublicResultJosn resultJosn = matchService.update(match);
		return resultJosn;
	}

	/**
	 * 模糊分页查询比赛信息
	 * 
	 * @param match
	 * @param page
	 * @param pageSize
	 * @return
	 */
	@GetMapping
	public LayuiTableResult select(Match match, @RequestParam(defaultValue = "1") Integer page,
			@RequestParam(defaultValue = "10") Integer pageSize) {
		LayuiTableResult result = matchService.query(match, page, pageSize);
		return result;
	}

	/**
	 * 根据比赛分页查找此比赛的所有场次
	 * 
	 * @param id
	 * @return
	 */
	@GetMapping(value = "/{id}")
	public PublicResultJosn selectById(@PathVariable String id) {
		// 校验
		if (StringUtils.isBlank(id)) {
			throw new IllegalArgumentException("id参数异常");
		}
		PublicResultJosn resultJosn = matchService.queryOne(id);
		return resultJosn;
	}

}
